package browser;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;

import main.Application;

import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import tools.Pair;
import exception.queryBuilder.AliasNotFound;
import exception.recordEdit.RecordEditNotFoundException;
import exception.recordEdit.SuchComponentExictException;

public class RecordEditFactory 
{
	private Pair<String,String>[] xmls;//<nazwa recEditki(zapewne jak tabeli),sciezka do xmla>
	private static RecordEditFactory instance;
	
	private RecordEditFactory()
	{
		xmls=Application.getInstance().getXMLsTable();
	}
	public static RecordEditFactory getInstance()
	{
		if(instance==null)
			instance=new RecordEditFactory();
		return instance;
	}
	/**
	 * bo moge chciec pobrac edytke,ktora nie jest zwiazana z zadna tabela, np przy ustawianiu parametrow wydruku
	 * @param recordEditName
	 * @param xmlFile
	 * @return
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 * @throws ParseException 
	 * @throws IOException 
	 * @throws SAXException 
	 * @throws AliasNotFound 
	 * @throws SuchComponentExictException 
	 * @throws RecordEditNotFoundException 
	 * @throws Exception
	 */
	public EditRecord getRecordEdit(String recordEditName,String xmlFile) throws RecordEditNotFoundException, SuchComponentExictException, AliasNotFound, SAXException, IOException, ParseException, SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException
	{				
		EditRecord recEd= createRecEdit(recordEditName,xmlFile);				
		return recEd;
	}
	public EditRecord getRecordEdit(String recordEditName) throws RecordEditNotFoundException, SuchComponentExictException, AliasNotFound, SAXException, IOException, ParseException, SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException
	{
		//if(recordEdits.get(recordEditName)!=null )
			//return recordEdits.get(recordEditName);
		
		EditRecord recEd= createRecEdit(recordEditName,"");
				
		return recEd;
	}
	public EditRecord getRecordEdit(String tableName, int search_mode)throws RecordEditNotFoundException, SuchComponentExictException, AliasNotFound, SAXException, IOException, ParseException, SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException 
	{
		EditRecord recEd= createRecEdit(tableName,"",search_mode);				
		return recEd;
	}
	public EditRecord getRecordEdit(String tableName, String xmlFile,int search_mode) throws RecordEditNotFoundException, SuchComponentExictException, AliasNotFound, SAXException, IOException, ParseException, SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException
	{
		EditRecord recEd= createRecEdit(tableName,xmlFile,search_mode);	
		return null;
	}
	private EditRecord createRecEdit(String recordEditName, String xmlFile,int runMode) throws RecordEditNotFoundException, SAXException, IOException, SuchComponentExictException, AliasNotFound, ParseException, SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException 
	{
		if(xmlFile.equals(""))
		{
			int i=0;
		
			for(;i<xmls.length;++i)
			{
				if(xmls[i].getLeft().equals(recordEditName))
					break;
			}
			if(i==xmls.length)
				throw new RecordEditNotFoundException(recordEditName);
			xmlFile=xmls[i].getRight();
		}	
		DOMParser xmlparser = new DOMParser(); 
		xmlparser.parse(xmlFile);
		Document doc = xmlparser.getDocument();
		
		return createRecEdit(doc,runMode);
	}
	private EditRecord createRecEdit(String recordEditName, String xmlFile) throws RecordEditNotFoundException, SAXException, IOException, SuchComponentExictException, AliasNotFound, ParseException, SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException 
	{
		return createRecEdit(recordEditName,xmlFile,EditRecord.INNY_MODE);
	}
	private EditRecord createRecEdit(Document doc,int runMode) throws SuchComponentExictException, AliasNotFound, ParseException, SQLException, SAXException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException 
	{
		return new EditRecord(doc,runMode);
	
		
	}
	
}
